import Vue from "vue";
import Vuex, { Store } from "vuex";
import axios from "axios";
Vue.use(Vuex);
export default new Store({
  actions: {
    // 加的地方
    jia({ commit }, value) {
      commit("JIA", value);
    },
    // 减的地方
    jian({ commit }, value) {
      commit("JIAN", value);
    },
    // 判断减
    jishuzaijia({ commit, state }, value) {
      if (state.sum % 2 === 1) {
        commit("JIA", value);
      }
    },
    // 延迟的加载
    dengyihui({ commit }, value) {
      setTimeout(() => {
        commit("JIA", value);
      }, 2000);
    },
    fuwuJia({ commit }) {
      axios.get("https://api.uomg.com/api/rand.qinghua?format=json").then(
        (res) => {
          commit("JIA", res.data.content.length);
          console.log(res.data.content);
        },
        (err) => {
          console.log(err.message);
        }
      );
    },
  },
  mutations: {
    JIA(state, value) {
      state.sum += value;
    },
    JIAN(state, value) {
      state.sum -= value;
    },
  },
  state: {
    sum: 1,
    school:'beijingyoudiandaxue',
    address:'海淀区'
  },
  // 数据加工
  getters:{
    bigSum(state){
      return state.sum * 10;
    },
    bigSchool(state){
      return state.school.toUpperCase();
    }
  }
});
